package com.huawei.pet.service.impl;

import com.huawei.basic.service.impl.BaseServiceImpl;
import com.huawei.pet.domain.Pet;
import com.huawei.pet.domain.PetDetail;
import com.huawei.pet.domain.PetOnlineAuditLog;
import com.huawei.pet.mapper.PetDetailMapper;
import com.huawei.pet.mapper.PetMapper;
import com.huawei.pet.mapper.PetOnlineAuditLogMapper;
import com.huawei.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raintea
 * @since 2022-05-19
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void save(Pet pet) {
        //设置所属店铺  已经在controller设置
        //保存宠物信息
        petMapper.save(pet);//返回自增ID
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPetId(pet.getId());
            //保存宠物详情
            petDetailMapper.save(detail);
        }
    }

    @Override
    public void update(Pet pet) {
        /*petMapper.update(pet);
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPetId(pet.getId());
            petDetailMapper.update(detail);
        }else{
            petDetailMapper.removeByPetId(pet.getId());
        }*/

    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }

    @Override
    public void onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            StringBuffer sb = new StringBuffer();
            //1.审核文本
           /* if(!BaiduAuditUtils.TextCensor(pet.getName())){
                sb.append(pet.getName()+"不合法！！");
            }
            if(!BaiduAuditUtils.ImgCensor("http://dfs.java.itsource.cn"+pet.getResources().split(",")[0])){
                sb.append("图片不合法！！");
            }*/
            if(sb.length() > 0){
                //1.1 审核不通过，记录审核日志 发个消息看你
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setNote(sb.toString());
                petOnlineAuditLogMapper.save(log);
                //SmsSendUtils.sendSmsMsg();  @TODO  自己做
                continue;
            }
            //1.2 审核通过，修改状态 + 上架时间
            pet.setState(1);
            pet.setOnsaletime(new Date());
            petMapper.update(pet);
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        //修改状态为下架
        //填写下架时间
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        petMapper.offSale(map);
    }
}
